﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using HZ.MBSM.DBUtility;
public partial class Deparment_Filse_Files_Update : System.Web.UI.Page
{
    private string strTitle, strContent;
    private long strAccountid;
    private bool isMust;
    string strEmailId;
    string IsReUpLoad, oldFilePath, oldFileTitle;

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumDeparment);
        strEmailId = SysFun.PasswordDecrypt(Request.QueryString["EmailId"].Trim());
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            strAccountid = _Account.AccountId;

            if (!IsPostBack)
            {
                ListInfor(strEmailId);
            }
            else
            {
                IsReUpLoad = Request.Form["isReUpLoad"].Trim();   //0表示不更改附件 1 表示重新上传附件
                oldFilePath = Request.Form["OldFilePath"].Trim();   //旧附件文件存储地址
                oldFileTitle = Request.Form["OldFileTitle"].Trim();   //旧附件文件标题  

                isMust = checkValue(Request);
                if (Request.Form["btnSubmit"] == "保  存")
                {
                    Save(1);
                }
                if (Request.Form["btnSubmit2"] == "发 送")
                {
                    Save(2);
                }
            }

        }
    }

    /// <summary>
    /// 显示信息
    /// </summary>
    /// <param name="strEmailId"></param>
    private void ListInfor(string strEmailId)
    {
        string sql = "select title,content,adddate,uploadfile,downLoadUrl from D_Email where EmailId=" + strEmailId;
        Db db = new Db();
        DataTable dt = db.DataTable(sql);
        if (dt.Rows.Count > 0)
        {
            txtName.Text = dt.Rows[0]["title"].ToString();
            txtContent.Text = dt.Rows[0]["content"].ToString();
            string htmlContent = "";
            if (dt.Rows[0]["uploadfile"].ToString().Trim() != "")
            {
                htmlContent = "<a target='_blank' href='" + dt.Rows[0]["downLoadUrl"].ToString() + "'><b>" + dt.Rows[0]["uploadfile"].ToString() + "</b></a>" +
                             "<input type='hidden' name='OldFilePath' id='OldFilePath' value='" + dt.Rows[0]["downLoadUrl"].ToString() + "'/>" +   //旧附件地址
                             "<input type='hidden' name='OldFileTitle' id='OldFileTitle' value='" + dt.Rows[0]["uploadfile"].ToString() + "'/>" +     //就附件标题
                             "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                            "<input type='button' name='ReUpLoad' id='ReUpLoad' value='重新上传' class='button_bg' style=\"width='60px'\" onclick=\"javascript:listFileUpLoad('fj')\"/>" +
                            "<input type='hidden' name='isReUpLoad' id='isReUpLoad' value='0'>";
            }
            else
            {
                htmlContent = "<b>无附件</b>" +
                             "<input type='hidden' name='OldFilePath' id='OldFilePath' value='" + dt.Rows[0]["downLoadUrl"].ToString() + "'/>" +   //旧附件地址
                             "<input type='hidden' name='OldFileTitle' id='OldFileTitle' value='" + dt.Rows[0]["uploadfile"].ToString() + "'/>" +     //就附件标题
                             "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                            "<input type='button' name='ReUpLoad' id='ReUpLoad' value='重新上传' class='button_bg' style=\"width='60px'\" onclick=\"javascript:listFileUpLoad('fj')\"/>" +
                            "<input type='hidden' name='isReUpLoad' id='isReUpLoad' value='0'>";
            }
            txtfj.Text = htmlContent;
          
           // HiddenField1.Value = dt.Rows[0]["info_sort"].ToString();
        }
    }

    /// <summary>
    /// 保存信息
    /// </summary>
    private void Save(int ff)
    {
        if (isMust)
        {
            string uploadFile = "";  //附件文件名标题
            string uploadFilePath = "";   //附件存储路径
            string downLoadUrl = "";   //下载地址

            if (FileUpload1.HasFile)
            {
                uploadFile = FileUpload1.FileName;
                DateTime dtime = DateTime.Now;
                string filename = dtime.ToShortDateString().Replace("-", "") +
                     "-" + dtime.Hour.ToString() + dtime.Minute.ToString() + dtime.Second.ToString() +
                     "-" + new Random().Next(10000).ToString() + new System.IO.FileInfo(uploadFile).Extension;
                uploadFilePath = System.Web.HttpContext.Current.Request.MapPath("../DupdateFile/") + filename;
                downLoadUrl = "../DupdateFile/" + filename;
                FileUpload1.SaveAs(uploadFilePath);
            }
            if (ff == 1)  //保存
            {
                int succ = 0;
                string procName = "P_DEmail_Add";
                SqlParameter[] para = new SqlParameter[6];
                para[0] = new SqlParameter("@title", strTitle);
                para[1] = new SqlParameter("@content", strContent);
                para[2] = new SqlParameter("@accountid", strAccountid);
                para[3] = new SqlParameter("@uploadfile", uploadFile);
                para[4] = new SqlParameter("@uploadfilepath", downLoadUrl);
                para[5] = new SqlParameter("@v_Success", succ);
                para[5].Direction = ParameterDirection.Output;
                Db db = new Db();
                db.ExecuteProcedure(procName, para);
                db.Close();
                succ = (int)para[5].Value;
                if (succ == 1)
                {
                    Response.Write("<script>alert('信息保存成功!');</script>");
                }
                else
                {
                    Response.Write("<script>alert('信息保存失败!')</script>");
                }
            }
            if (ff == 2) //发送
            {
                if (Request.Form["textsend"] == "")
                {
                    Response.Write("<script>alert('发送人不能为空!')</script>");
                    return;
                }
                int succ = 0;
                string Accoumtid_Send = Request.Form["HiddenSend"].ToString().Trim().Remove(0, 1);
                string procName = "P_DEmail_Send";
                SqlParameter[] para = new SqlParameter[7];
                para[0] = new SqlParameter("@title", strTitle);
                para[1] = new SqlParameter("@content", strContent);
                para[2] = new SqlParameter("@accountid", strAccountid);
                para[3] = new SqlParameter("@uploadfile", uploadFile);
                para[4] = new SqlParameter("@uploadfilepath", downLoadUrl);
                para[5] = new SqlParameter("@Accoumtid_Send", Accoumtid_Send);
                para[6] = new SqlParameter("@v_Success", succ);
                para[6].Direction = ParameterDirection.Output;
                Db db = new Db();
                db.ExecuteProcedure(procName, para);
                db.Close();
                succ = (int)para[6].Value;
                if (succ == 1)
                {
                    Response.Write("<script>alert('信息保存成功!');self.close();</script>");
                }
                else
                {
                    Response.Write("<script>alert('信息保存失败!')</script>");
                }
            }
        }
        else
        {
            Response.Write("<script>alert('请将信息填写完整!!!')</script>");
        }
    }

    /// <summary>
    /// 验证必填项是否有空
    /// </summary>
    /// <returns></returns>
    private bool checkValue(HttpRequest request)
    {
        strTitle = txtName.Text.Trim();
        strContent = txtContent.Text;
      


        if (strTitle == "" || strContent == "" )
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
